﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using FlotasDesktop;
using System.Globalization;
using System.Security.Cryptography;

namespace GestorDeFlotasDesktop.Login
{
    public partial class Login : Form
    {
        String conexion;
        int contador = 0;
        String nombre = "";
        public Login()
        {
            InitializeComponent();
            Utils utils = new Utils();
            conexion = utils.cargarProperties();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        string sql = "select * from TEQUILASO.Usuario where Nombre = '" + this.textBoxUsuario.Text + "'";
                        connection.Open();
                        command.Connection = connection;
                        command.CommandText = sql;
                        command.CommandType = CommandType.Text;

                        SqlDataReader reader = command.ExecuteReader();
                        if (reader.HasRows)
                        {
                            reader.Read();

                            if ((bool)reader["Habilitado"])
                            {
                                if (this.validarContraseña(this.textBoxContraseña.Text, (string)reader["Password"]))
                                {
                                    MessageBox.Show("Bienvenido " + this.textBoxUsuario.Text , "Ingreso de Usuario");
                                    connection.Close();
                                    Form1 form1 = new Form1(this.textBoxUsuario.Text);
                                    this.Visible = false;
                                    form1.Show();
                                }
                                else
                                {
                                    MessageBox.Show("Contraseña incorrecta");
                                    
                                    if (nombre.Length == 0)
                                    {
                                        nombre = textBoxUsuario.Text;
                                        contador++;
                                    }
                                    else
                                    {
                                        if (nombre.Equals(textBoxUsuario.Text))
                                        {
                                            contador++;
                                            if (contador == 3)
                                            {
                                                this.inhabilitarUsuario();
                                                MessageBox.Show("Usuario dado de baja");
                                                contador = 0;
                                                nombre = "";
                                            }
                                        }
                                        else
                                        {
                                            nombre = textBoxUsuario.Text;
                                            contador = 1;
                                        }
                                    }

                                    
                                }
                            }
                            else
                            {
                                MessageBox.Show("Usuario inhabilitado");
                            }
                        }
                        else
                        {
                            MessageBox.Show("Usuario incorrecto");
                        }
                        reader.Close();
                        
                    }
                }
                catch (Exception er)
                {
                    Console.Write("Ocurrio un error al consultar el STORE.");
                }
                
            }
        }

        public bool validarContraseña(string cadena,string hash)
        {
            SHA256 sha256 = new SHA256Managed();
            byte[] inputBytes = Encoding.UTF8.GetBytes(cadena);
            byte[] hashedBytes = sha256.ComputeHash(inputBytes);
            StringBuilder hashedString = new StringBuilder();
            string cadenaHash;

            for (int i = 0; i < hashedBytes.Length; i++)
                hashedString.Append(hashedBytes[i].ToString("x2").ToLower());

            cadenaHash = hashedString.ToString();

            for (int i = 0; i < cadenaHash.Length; i++)
            {
                if (!(cadenaHash[i].Equals(hash[i])))
                    return false;
            }
            
            return true;
        }

        public void inhabilitarUsuario()
        {
            using (SqlConnection connection2 = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command2 = new SqlCommand())
                    {

                        string sql = "update tequilaso.usuario set habilitado = 0 where nombre = '" + textBoxUsuario.Text + "'";
                        connection2.Open();
                        command2.Connection = connection2;
                        command2.CommandText = sql;
                        command2.CommandType = CommandType.Text;
                        command2.ExecuteReader();
                        connection2.Close();

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

    }
}
